package com.example.yblin.hello24;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.yblin.hello24.DataUtils.RanklistDataUtil;

public class RankActivity extends AppCompatActivity {



    private TextView tvTitle;
    private TextView btToggle;
    private ImageView btBack;
    private TextView tvView;
    private TextView textView;
    private RecyclerView recyclerView;
    private ProgressBar progressBar;
    private RanklistAdapter ranklistAdapter;
    private RanklistDataUtil ranklistDataUtil;
    private int mode = RanklistDataUtil.MODE_STANDARD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);

        btBack = findViewById(R.id.back_rank_list);

        recyclerView = findViewById(R.id.recyclerview_rank_list);
        progressBar = findViewById(R.id.progressbar_rank_list);

        tvTitle = findViewById(R.id.textView_title_rank_list);
        btToggle = findViewById(R.id.textView_toggle_rank_list);

        recyclerView.setHasFixedSize(true); //固定大小
        //线性布局管理器并设置
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        recyclerView.setLayoutManager(layoutManager);
        ranklistAdapter = new RanklistAdapter();
        recyclerView.setAdapter(ranklistAdapter);
        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mode == RanklistDataUtil.MODE_STANDARD) {
                    mode = RanklistDataUtil.MODE_STAGE;
                    refresh();
                } else {
                    mode = RanklistDataUtil.MODE_STANDARD;
                    refresh();
                }
            }
        });

        refresh();//刷新排行榜
    }

    private void refresh() {
        RanklistAsync async = new RanklistAsync();
        async.execute();
    }


    //异步任务里面网络获取
    public class RanklistAsync extends AsyncTask<Void, Void, Integer> {

        @Override
        protected Integer doInBackground(Void... voids) {
            ranklistDataUtil = new RanklistDataUtil();
            ranklistDataUtil.setMode(mode);
            return ranklistDataUtil.achieveFromInternet(RankActivity.this);
        }

        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
            btToggle.setEnabled(false);
        }

        @Override
        protected void onPostExecute(Integer integer) {
            //进度条消失
            progressBar.setVisibility(View.INVISIBLE);
            btToggle.setEnabled(true);
            //自动刷新界面
            ranklistAdapter.setRanklistData(ranklistDataUtil.getRankItemDataList(), mode);

            if (mode == RanklistDataUtil.MODE_STANDARD) {
                tvTitle.setText("排行榜（标准模式）");
            } else {
                tvTitle.setText("排行榜（闯关模式）");
            }
        }
    }
}
